package com.gg.widget.navigationbar

import android.annotation.SuppressLint
import android.content.Context
import android.support.annotation.ColorInt
import android.text.TextUtils
import android.view.View
import android.view.ViewGroup

import com.gg.widget.R


/**
 * Creator : GG
 * Time    : 2017/11/13
 * Mail    : gg.jin.yu@gmail.com
 * Explain :
 */

class DefaultNavigationBar(params: Builder.DefaultNavigationParams) : AbsNavigationBar<DefaultNavigationBar.Builder.DefaultNavigationParams>(params) {

    override fun bindLayoutId(): Int = R.layout.title_bar

    override fun applyViews() {
        setText(R.id.middle_text, "title")

        setIcon(R.id.left_img, params.mLeftIconId)

        setIcon(R.id.middle_img, params.mMiddleIconId)

        setIcon(R.id.right_img, params.mRightIconId)

        setText(R.id.left_text, params.mLeftText)

        setText(R.id.middle_text, params.mMiddleText)

        setText(R.id.right_text, params.mRightText)
        if (params.color != 0)
            findViewById<View>(R.id.titleBar).setBackgroundColor(params.color)
        if (params.middleTextColor != 0)
            setTextColor(R.id.middle_text, params.middleTextColor)


        if (params.statusBarHeight != 0) {
            findViewById<View>(R.id.statusBar).apply {
                if (params.statusBarColor != 0)
                    setBackgroundColor(params.statusBarColor)
                layoutParams.apply {
                    height = params.statusBarHeight
                }
            }
        }

        if (params.mLeftClickListener != null) {
            setOnClickListener(R.id.left_view, params.mLeftClickListener)
        }

        if (params.mMiddleClickListener != null) {
            setOnClickListener(R.id.middle_view, params.mLeftClickListener)
        }

        if (params.mRightClickListener != null) {
            setOnClickListener(R.id.right_view, params.mRightClickListener)
        }

        if (!TextUtils.isEmpty(params.mText))
            setText(params.mViewId, params.mText)

        if (params.mClickListener != null)
            setOnClickListener(params.mViewId, params.mClickListener)

    }

    @SuppressLint("WrongViewCast")
    fun getContentView(): View {
        return findViewById(R.id.titleBar)
    }

    class Builder : AbsNavigationBar.Builder {

        var params: DefaultNavigationParams

        constructor(context: Context, parent: ViewGroup) : super(context, parent) {
            params = DefaultNavigationParams(context, parent)
        }

        constructor(context: Context) : super(context) {
            params = DefaultNavigationParams(context, null)
        }

        fun setText(viewId: Int, text: String): Builder {
            params.mViewId = viewId
            params.mText = text
            return this
        }


        fun setClickListener(viewId: Int, listener: View.OnClickListener): Builder {
            params.mViewId = viewId
            params.mClickListener = listener
            return this
        }


        override fun create(): DefaultNavigationBar {
            return DefaultNavigationBar(params)
        }

        fun setLeftText(text: String): Builder {
            params.mLeftText = text
            return this
        }

        fun setMiddleText(text: String): Builder {
            params.mMiddleText = text
            return this
        }

        fun setMiddleTextColor(color: Int): Builder {
            params.middleTextColor = color
            return this
        }

        fun setRightText(text: String): Builder {
            params.mRightText = text
            return this
        }

        fun setLeftIcon(imgId: Int): Builder {
            params.mLeftIconId = imgId
            return this
        }

        fun setBackgroundColor(@ColorInt color: Int): Builder {
            params.color = color
            return this
        }

        fun setMiddleIcon(imgId: Int): Builder {
            params.mMiddleIconId = imgId
            return this
        }

        fun setRightIcon(imgId: Int): Builder {
            params.mRightIconId = imgId
            return this
        }

        fun setStatusBarHeight(height: Int): Builder {
            params.statusBarHeight = height
            return this
        }

        fun setStatusBarColor(@ColorInt color: Int): Builder {
            params.statusBarColor = color
            return this
        }


        fun setLeftClickListener(listener: View.OnClickListener): Builder {
            params.mLeftClickListener = listener
            return this
        }

        fun setMiddleClickListener(listener: View.OnClickListener): Builder {
            params.mMiddleClickListener = listener
            return this
        }

        fun setRightClickListener(listener: View.OnClickListener): Builder {
            params.mRightClickListener = listener
            return this
        }

        inner class DefaultNavigationParams(context: Context, parent: ViewGroup?) : AbsNavigationBar.Builder.AbsNavigationParams(context, parent) {

            var mLeftText: String? = null
            var mMiddleText: String? = null
            var mRightText: String? = null
            var mLeftClickListener: View.OnClickListener? = null
            var mMiddleClickListener: View.OnClickListener? = null
            var mRightClickListener: View.OnClickListener? = null
            var mViewId: Int = 0
            var mText: String? = null
            var mClickListener: View.OnClickListener? = null
            var mLeftIconId = 0
            var mMiddleIconId = 0
            var mRightIconId = 0
            var color = 0
            var middleTextColor = 0
            var statusBarHeight = 0
            var statusBarColor = 0
        }
    }
}
